/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.basis.xml;

import java.io.IOException;
import java.io.OutputStream;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import de.mnit.basis.fehler.Fehler;
import de.mnit.basis.fehler.system.F_Sys_InOut;
import de.mnit.basis.sys.Ausgabe;

public class XMLAusgabe {

	public static void main(String[] args) {
		try {
			XMLAusgabe xml=new XMLAusgabe(Ausgabe.streamText());
			xml.oeffnen("party");
			xml.attribut("datum", "31.12.2001");

			xml.oeffnen("gast");
			xml.attribut("name", "Albert Ängstlich");
			xml.schliessen();

			xml.schliessen();
			xml.fertig();
		}
		catch(Throwable t) {
			Fehler.zeig(t);
		}
	}


	private XMLStreamWriter writer;
	private OutputStream stream;


	public XMLAusgabe(OutputStream os) throws F_Sys_InOut {
		try {
			XMLOutputFactory factory = XMLOutputFactory.newInstance();
			this.writer = factory.createXMLStreamWriter(os);
			this.writer.writeStartDocument();
			this.writer.writeCharacters("\n");
			this.stream=os;
		}
		catch(XMLStreamException e) {
			Fehler.weitergeben(e, "Schreiben fehlgeschlagen");
		}
	}

	public XMLAusgabe(OutputStream os, String encoding) throws F_Sys_InOut {
		try {
			XMLOutputFactory factory = XMLOutputFactory.newInstance();
			this.writer = factory.createXMLStreamWriter(os);
			this.writer.writeStartDocument(encoding, "1.0");
			this.stream=os;
		}
		catch(XMLStreamException e) {
			Fehler.weitergeben(e, "Schreiben fehlgeschlagen");
		}
	}


	public void fertig() throws F_Sys_InOut {
		try {
			this.writer.writeEndDocument();
			this.writer.close();
			this.stream.close();
		}
		catch(XMLStreamException e) {
			Fehler.weitergeben(e, "Schreiben fehlgeschlagen");
		}
		catch(IOException e) {
			Fehler.weitergeben(e, "Schreiben fehlgeschlagen");
		}
	}

	public void oeffnen(String name) throws F_Sys_InOut {
		try {
			this.writer.writeStartElement(name);
//			this.writer.writeCharacters("\n"); //Führt zu Fehlern!!!
		}
		catch(XMLStreamException e) {
			Fehler.weitergeben(e, "Schreiben fehlgeschlagen");
		}
	}

	public void text(String text) throws F_Sys_InOut {
		try {
			writer.writeCharacters(text);
		}
		catch(XMLStreamException e) {
			Fehler.weitergeben(e, "Schreiben fehlgeschlagen");
		}
	}

	public void schliessen(String wirdnichtverwendet) throws F_Sys_InOut {
		this.schliessen();
	}

	public void schliessen() throws F_Sys_InOut {
		try {
			this.writer.writeEndElement();
			this.writer.writeCharacters("\n");
		}
		catch(XMLStreamException e) {
			Fehler.weitergeben(e, "Schreiben fehlgeschlagen");
		}
	}

	public void attribut(String name, String wert) throws F_Sys_InOut {
		try {
			this.writer.writeAttribute(name, wert);
		}
		catch(XMLStreamException e) {
			Fehler.weitergeben(e, "Schreiben fehlgeschlagen");
		}
	}

	public void attributNull(String name, String wert) throws F_Sys_InOut {
		if(wert!=null)
			attribut(name, wert);
	}

	public XMLStreamWriter gWriter() {
		return this.writer;
	}

	public void schnell(String name, String text) throws F_Sys_InOut {
		oeffnen(name);
		text(text);
		schliessen();
	}

}
